package com.wythe.mall.ui;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wythe.mall.R;

public class DetailedFragment extends Fragment {

    private WebView mainWebView;
    private View view;
    private String mUrl;

    public static DetailedFragment getInstance(String url) {
        DetailedFragment fragment = new DetailedFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mUrl = bundle.getString("url");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        view = inflater.inflate(R.layout.fragment_goods_detailed, viewGroup, false);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mainWebView != null) {
            mainWebView.onResume();
        }

        initData();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mainWebView != null) {
            mainWebView.onPause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        if (mainWebView != null) {
            mainWebView.stopLoading();
            mainWebView.removeAllViews();
            mainWebView.destroy();
        }
        super.onDestroy();
    }

    public void initView() {
        mainWebView = view.findViewById(R.id.mainWebView);
        initWebView(mainWebView);
    }
    public void initWebView(WebView webview) {
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webview.getSettings().setDefaultTextEncodingName("utf-8");
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setDatabaseEnabled(true);
        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        }


        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

            }
        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null || view == null) return true;

                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onGetWebContentHeight();
                    }
                }, 1000);
            }
        });

        // 添加客户端支持
        webview.setWebChromeClient(new WebChromeClient());

    }

    public void initData() {

//        mainWebView.loadUrl("https://upload-images.jianshu.io/upload_images/1797490-012666441f452c00.png");
        mainWebView.loadDataWithBaseURL(null,mUrl, "text/html" , "utf-8", null);//加载html数据
    }

    public void onGetWebContentHeight() {
        mainWebView.post(new Runnable() {
            @Override
            public void run() {
                mainWebView.measure(0, 0);
                int measureHeight = mainWebView.getMeasuredHeight();
                ViewGroup.LayoutParams layoutParams = mainWebView.getLayoutParams();
                layoutParams.height = measureHeight;
                mainWebView.setLayoutParams(layoutParams);
            }
        });
    }
}
